package pluginUtils;

import java.io.PrintStream;

public class TorusVillarceauCircle {
	
	private Torus t; 
	private double th; 
	private double epsilon; 
	
	public TorusVillarceauCircle(Torus t, double th, double epsilon) {
		super();
		this.t = t;
		this.th = th;
		this.epsilon = epsilon;
	}

	public Pos3D getPoint(double ang){
		return new Pos3D(valX(ang), valY(ang), valZ(ang));
	}
	
	public double valX(double ang){
		double gR=this.t.getGR();
		double pr=this.t.getPr(); 
		double terme1=Math.sqrt(gR*gR-pr*pr)*Math.sin(ang)*Math.cos(th);
		double terme2=Math.sin(th)*(pr+gR*Math.cos(ang)); 
		return terme1-terme2; 
	}
	
	public double valY(double ang){
		double gR=this.t.getGR();
		double pr=this.t.getPr(); 
		double terme1=Math.sqrt(gR*gR-pr*pr)*Math.sin(ang)*Math.sin(th);
		double terme2=Math.cos(th)*(pr+gR*Math.cos(ang)); 
		return epsilon*(terme1+terme2); 
	}
	public double valZ(double ang){
		return this.t.getPr()*Math.sin(ang); 
	}
	
	public Circle3D getCircle3D(){
		//output.println("getCircle3D"); 
		Pos3D p1=this.getPoint(0); 
		Pos3D p2=this.getPoint(Math.PI/3);
		Pos3D p3=this.getPoint(2*Math.PI/3);
		System.out.println("p1 "+p1); 
		System.out.println("p2 "+p2); 
		System.out.println("p3 "+p3); 
		
		Circle3D prov=Circle3D.buildCircle3D(p1, p2, p3);
		return prov; 
	}
public static void main(String[] args) throws Exception
{
	PrintStream output=new PrintStream("/tmp/viltorus.txt");	
	Torus hector=new Torus(6,6/1.618); 
	
	
	 int nbCercles=40; 
		output.println(nbCercles+","); 
		for(int i=0;i<nbCercles;i++){
		double angle=2*i*Math.PI/nbCercles; 
		TorusVillarceauCircle cv=new TorusVillarceauCircle(hector,angle, 1);
		Circle3D cercle=cv.getCircle3D();
		double rayon=cercle.getRayon(); 
		Pos3D centre=cercle.getCenter(); 
		Plane p=cercle.getPlan(); 
		Plane p2=null; 
		Pos3D n1=p.getVecteurNormal(); 
		output.println(rayon+","); 
		System.out.println(i+" "+rayon); 
		output.println(centre.getX()+","); 
		output.println(centre.getY()+","); 
		output.println(centre.getZ()+","); 
		double angle1=Math.atan2(p.getC(),p.getA());
		double angle2=Math.atan2(Math.sqrt(p.getA()*p.getA()+p.getC()*p.getC()),p.getB()); 
		output.println(180*angle1/Math.PI+",");
		output.println(180*angle2/Math.PI+",");
		// dans l'autre sens
		
		cv=new TorusVillarceauCircle(hector,angle, -1);
		cercle=cv.getCircle3D();
		rayon=cercle.getRayon(); 
		centre=cercle.getCenter(); 
		p=cercle.getPlan(); 
		n1=p.getVecteurNormal(); 
		output.println(rayon+","); 
		System.out.println(i+" "+rayon); 
		output.println(centre.getX()+","); 
		output.println(centre.getY()+","); 
		output.println(centre.getZ()+","); 
		angle1=Math.atan2(p.getC(),p.getA());
		angle2=Math.atan2(Math.sqrt(p.getA()*p.getA()+p.getC()*p.getC()),p.getB()); 
		output.println(180*angle1/Math.PI+",");
		output.println(180*angle2/Math.PI+",");
	
		}
		output.close(); 
	
	
	
	
	
	
	
	
	
	
}
}

